package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peppa.petshop.entity.Category;
import com.peppa.petshop.entity.Customer;
import com.peppa.petshop.mapper.CustomerMapper;
import com.peppa.petshop.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired(required = false)
    CustomerMapper customerMapper;

    @Override
    public DataVO<Customer> customerList(Integer page, Integer limit) {
        if (page== null){
            page=1;
            limit = 10;
        }
        IPage<Customer> customerIPage = new Page<>(page,limit);

        IPage<Customer> result = customerMapper.selectAll(customerIPage);
        List<Customer> categories = result.getRecords();
        Integer count = (int) result.getTotal();
        DataVO<Customer> customerDataVO = new DataVO<>(0,"",count,categories);
        return customerDataVO;
    }

    @Override
    public Customer login(String phone, String password) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone",phone);
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        wrapper.eq("password",password);
        Customer customer = customerMapper.selectOne(wrapper);
        return customer;
    }

    @Override
    public void deleteById(Integer id) {
        Customer customer = customerMapper.selectById(id);
        if (customer.getDeleted() == 0){
            customerMapper.deleteById(id);
        }else{
            customerMapper.unlocked(id);
        }
    }

    @Override
    public void resetById(Integer id) {
        Customer customer = customerMapper.selectById(id);
        Map<String, String> map = new HashMap<>();
        if (customer.getDeleted() == 1){
            map.put("msg","请检查用户是否锁定");
        }else {
            customer.setPassword("666666");
            updateById(customer);
            map.put("msg","重置成功");
        }
    }

    @Override
    public void unlockById(Integer id) {
        customerMapper.unlocked(id);
    }

    @Override
    public void renewPassword(String old_password, String new_password, String again_password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_manager",1);
        Customer customer = customerMapper.selectOne(queryWrapper);
        customer.setPassword(new_password);
        customerMapper.update(customer,queryWrapper);
    }


}
